package com.hlkj.cartservice.controller;

import com.hlkj.cartservice.service.TbBrandService;
import com.hlkj.pojo.common.TbBrand;
import com.hlkj.pojo.entity.PageResult;
import com.hlkj.pojo.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TbBrand的controller，给其它服务提供使用
 */
@RestController
public class TbBrandController {
    @Autowired
    private TbBrandService tbBrandService;

    @GetMapping(value = "/getAllBrands")
    public List<TbBrand> getAllBrands() {
        return tbBrandService.findAllBrands();
    }

    @PostMapping(value = "/addBrand")
    public Result addBrand(@RequestBody TbBrand brand) {
        try {
            tbBrandService.addBrand(brand);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    @PostMapping(value = "/updateBrand")
    public Result updateBrand(@RequestBody TbBrand brand) {
        try {
            tbBrandService.updateBrand(brand);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    @GetMapping(value = "/findById")
    public TbBrand findById(@RequestParam Long id) {
        return tbBrandService.findBrandById(id);
    }

    @PostMapping(value = "/deleteByIds")
    public Result deleteById(@RequestParam Long[] ids) {
        try {
            tbBrandService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    /**
     * 返回全部列表
     *
     * @return 分页结果
     */
    @GetMapping("/findPage")
    public PageResult findPage(@RequestParam int page, @RequestParam int rows) {
        return tbBrandService.findPage(page, rows);
    }

    /**
     * 返回全部列表
     *
     * @return 分页结果
     */
    @PostMapping("/search")
    public PageResult findPage(@RequestBody TbBrand brand, @RequestParam int page, @RequestParam int rows) {
        return tbBrandService.findPage(brand, page, rows);
    }
}
